package cn.mibcxb.java.dolphin.client;

import java.net.InetAddress;

import org.apache.commons.lang3.StringUtils;

import cn.mibcxb.java.dolphin.common.Command;
import cn.mibcxb.java.dolphin.common.DolphinData;
import cn.mibcxb.java.dolphin.exception.DolphinException;
import cn.mibcxb.java.dolphin.gui.Window;

public class Dolphin {
	private DolphinConsole console;
	private int id;
	private String name;
	private InetAddress address;
	private boolean isConnected = false;
	private boolean isShowing = false;
	private Window window;

	public Dolphin(DolphinConsole console, int id, String name,
			InetAddress address) {
		super();
		this.console = console;
		this.id = id;
		this.name = name;
		this.address = address;
		open();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public InetAddress getAddress() {
		return address;
	}

	public void setAddress(InetAddress address) {
		this.address = address;
	}

	private void open() {
		if (!isShowing) {
			window = new Window(this, name);
			window.setTitle(name);
			window.show();
		}
		isShowing = (null != window) && window.isShowing();
	}

	public void reopen() {
		open();
	}

	private void close() {
		isShowing = false;
		window = null;
	}

	public boolean isShowing() {
		return isShowing;
	}

	public void connect() {
		DolphinData data = getDolphinData(Command.CONNECT);
		try {
			data.setMsg(console.getmUser().getmName());
			console.send(data);
		} catch (DolphinException e) {
			e.printStackTrace();
		}
	}

	public void disconnect() {
		isConnected = false;
		DolphinData data = getDolphinData(Command.DISCONNECT);
		console.send(data);
		close();
	}

	public void chat(String msg) {
		if (!isConnected) {
			window.printMessage("Offline!");
		}
		DolphinData data = getDolphinData(Command.CHAT);
		try {
			data.setMsg(msg);
		} catch (DolphinException e) {
			e.printStackTrace();
		}
		console.send(data);
	}

	public void receive(DolphinData data) {
		if (!data.getName().equals(name) && isShowing()) {
			window.setTitle(data.getName());
		}
		Command cmd = data.getCmd();
		switch (cmd) {
		case CHAT:
			String msg = data.getMsg();
			if (isShowing() && StringUtils.isNotBlank(msg)) {
				window.printMessage(msg);
			}
			break;
		case CONNECT:
			DolphinData replyData = getDolphinData(Command.REPLY);
			console.send(replyData);
			break;
		case REPLY:
			String name = data.getName();
			if (StringUtils.isNotBlank(name)) {
				window.setTitle(name);
				window.setmName(name);
			}
			isConnected = true;
			break;
		case DISCONNECT:
			isConnected = false;
			break;
		default:
			break;
		}
	}

	private DolphinData getDolphinData(Command cmd) {
		return new DolphinData(cmd, address, console.getmUser().getmName());
	}
}
